package com.shp.controller;

import com.shp.pojo.Views;
import com.shp.service.GoodsService;
import com.shp.service.ViewsService;
import com.shp.utils.IpUtil;
import com.shp.utils.SaveViewsFile;
import com.shp.utils.ViewData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2020/3/18 18:31
 */
@Controller
public class ToController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ViewsService viewsService;

    /**
     * 首页显示商品，每一类商品查询6件，根据最新上架排序
     */
    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String homeGoods(Model model) {
        goodsService.setGoodsMsgOnIndex(model);
        return "/goods/home";
    }

    /**
     * 跳转到登陆页面
     *
     * @return login
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "/login";
    }

    /**
     * 跳转注册页面
     *
     * @return register
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register() {
        return "/register";
    }

    /**
     * 记录网站访问记录
     *
     * @param request HttpServletRequest
     */
    @RequestMapping(value = "/look", method = RequestMethod.POST)
    public void lookNum(@RequestBody Views views1, HttpServletRequest request) {
        System.out.println(views1);
        // 获取sessionId
        String sessionId = request.getSession().getId();
        Views views = ViewData.viewsMap.get(sessionId);
        // 获取时间戳
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        String times = null;
        // 具体时间格式
        String strTime = timestamp.toString();
        // 时间戳
        String longTime = String.valueOf(timestamp.getTime());
        if (views == null) {
            // 游客第一次访问
            views = new Views();
            views.setTimes(strTime);
            views.setSessionId(sessionId);
            views.setFirst_time(strTime);
            views.setIsFirst(false);
            // 保存游客
            ViewData.viewsMap.put(sessionId,views);
            // 获取请求者的ip
            String ip = IpUtil.getIp(request);
            views.setUri(views1.getUri());
            views.setIp(ip);
            // 保存到数据库
            viewsService.saveViews(views);
        }else{
            times = views.getTimes();
            times += " " + strTime;
            views.setTimes(times);
        }
        views.setFrequency(views.getFrequency() + 1);
        views.setUri(views1.getUri());
        views.setLast_time(strTime);
        // 获取请求者的ip
        String ip = IpUtil.getIp(request);
        views.setIp(ip);
        // 替换
        ViewData.viewsMap.replace(sessionId,views);
        // 写入到文件
        SaveViewsFile.save(ip,views,request);
    }
}
